package gr.atc.epart.googlewrapper.accounts;


import gr.atc.epart.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class GoogleLoginDialog extends Dialog
{

	 private String _info;
	 private String _initialusername;
	 private OnCompleteListener _completionListener;
	 private TextView	_user;
	 private TextView	_pwd;
	 
	 public interface OnCompleteListener 
	 {
	        public void login(String username,String password);
	        public void cancel();
	 }
	
	 
	 public GoogleLoginDialog(Context context, String info, String username, OnCompleteListener completionListener) 
	 {
	        super(context);
	        _info = info;
	        _initialusername = username;
	        _completionListener = completionListener;
	 }
	 
	 
	 @Override
	   public void onCreate(Bundle savedInstanceState) 
	 {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.logindialog);
	        
	        setTitle("Google Login");
	        TextView infotext =  (TextView)findViewById(R.id.LoginInfoText);
	        infotext.setText(_info);
	        
	        _user = (TextView)findViewById(R.id.LoginUserName);
	        _pwd = (TextView)findViewById(R.id.LoginPassword);
	        
	        if (_initialusername != null)
	        {
	        	_user.setText(_initialusername);
	        	_pwd.requestFocus();
	        }
	        else
	        {
	        	_user.requestFocus();
	        }
	        
	        Button loginbutton = (Button) findViewById(R.id.LoginButton);
	        Button cancelloginbutton = (Button) findViewById(R.id.CancelLoginButton);
	        
	        android.view.View.OnClickListener  loginClicked = new android.view.View.OnClickListener()
	        {
	        	@Override
	        	public void onClick(View view)
	        	{
	        		String user = String.valueOf(_user.getText());
	        		String pwd = String.valueOf(_pwd.getText());
	        		
	        		
	        		_completionListener.login(user, pwd);
	        		GoogleLoginDialog.this.dismiss();
	        	}
	        };

	        android.view.View.OnClickListener  loginCancelledClicked = new android.view.View.OnClickListener()
	        {
	        	@Override
	        	public void onClick(View view)
	        	{
	        		
	        		_completionListener.cancel();
	        		GoogleLoginDialog.this.dismiss();
	        	}
	        };

	        loginbutton.setOnClickListener(loginClicked);
	        cancelloginbutton.setOnClickListener(loginCancelledClicked);
	    }
	 
}
